/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import GUI.MainFunction;
import GUI.OptionMessage;
import connection.ManLabConnection;
import entities.BillingDetails;
import entities.BillingTestDetail;
import entities.DoctorDetails;
import entities.PatientDetails;
import entities.ReportDetails;
import entities.StaffDetail;
import entities.Test;
import entities.TestDetail;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VietLH
 */
public class ReportManage {

    private ArrayList<ReportDetails> reportList;
    private ArrayList<PatientDetails> patientList;
    private ArrayList<Test> testList;
    private ArrayList<DoctorDetails> doctorList;
    private ArrayList<StaffDetail> staffList;
    private ManLabConnection cnn;
    private ResultSet rs;
    private String qry;
    private MainFunction main;
    private OptionMessage optionMessage;
    private SimpleDateFormatter sdf;

    public ReportManage(ArrayList<ReportDetails> reportList, ArrayList<PatientDetails> patientList,
            ArrayList<Test> testList, ArrayList<DoctorDetails> doctorList, ArrayList<StaffDetail> staffList, MainFunction main) {
        cnn = new ManLabConnection();
        this.reportList = new ArrayList<ReportDetails>();
        this.doctorList = new ArrayList<DoctorDetails>();
        this.staffList = new ArrayList<StaffDetail>();
        this.testList = new ArrayList<Test>();
        this.reportList = new ArrayList<ReportDetails>();
        this.reportList = reportList;
        this.patientList = patientList;
        this.testList = testList;
        this.doctorList = doctorList;
        this.staffList = staffList;
        this.main = main;
        this.sdf = new SimpleDateFormatter();
        optionMessage = new OptionMessage(main);
    }

    public ReportManage(ArrayList<ReportDetails> reportList, MainFunction main) {
        cnn = new ManLabConnection();
        this.reportList = reportList;
        this.main = main;
        optionMessage = new OptionMessage(main);
    }

    public ReportManage() {
    }

    public ArrayList<ReportDetails> getAllReports() {
        return this.reportList;
    }

    public ReportDetails getSingleReport(int rID) {
        for (ReportDetails details : reportList) {
            if (details.getReportID() == rID) {
                return details;
            }
        }
        return null;
    }

    public boolean createNewReportFromBill(BillingDetails bd) {
        PatientDetails patient = bd.getPatientDetails();
        boolean cn = cnn.createConnection();
        for (BillingTestDetail billTestDetails : bd.getBillingTestDetail()) {
            Test test = billTestDetails.getTest();
            ReportDetails rd = new ReportDetails();
            rd.setTest(test);
            rd.setPatientDetails(patient);
            rd.setStatus(-1);
//            boolean cr=createNewReport(rd);

            qry = "insert into Report(TestID,pID) values(" + rd.getTest().getTestID() + "," + rd.getPatientDetails().getpID() + ")";
            boolean pass = cnn.executeQuery(qry);
            
            if (!pass) {
                return false;
            }
            this.reportList.add(rd);
            resetGUI();
        }
        
        cnn.closeConnection();
        return true;
    }

    public boolean createNewReport(ReportDetails rd) {
        boolean cn = cnn.createConnection();
        qry = "insert into Report(TestID,pID) values(" + rd.getTest().getTestID() + "," + rd.getPatientDetails().getpID() + ")";
        boolean pass = cnn.executeQuery(qry);
        cnn.closeConnection();
        if (pass) {
            this.reportList.add(rd);
            resetGUI();
            optionMessage.showMessage("New report has been created successfully!");
            return true;
        } else {
            optionMessage.showErrorMessage("Report has not been created! Please try again later!");
            return false;
        }
    }

    public boolean editReport(ReportDetails rd) {
        boolean cn = cnn.createConnection();
        int dID = 0;
        int sID = 0;
        if (rd.getDoctorDetails().getdID() != 0) {
            dID = rd.getDoctorDetails().getdID();
        }
        if (rd.getStaffDetail().getStaffID() != 0) {
            sID = rd.getStaffDetail().getStaffID();
        }
        if (dID > 0 && sID > 0) {
            qry = "update Report set testID=" + rd.getTest().getTestID()
                    + ",pID=" + rd.getPatientDetails().getpID() + ",dID=" + dID + ",staffID=" + sID
                    + ",report='" + rd.getReport()
                    + "', status=" + rd.getStatus() + ",testDate='" + sdf.convertDateToString(rd.getTestDate()) + "' where reportID=" + rd.getReportID();
        } else if (dID > 0 && sID <= 0) {
            qry = "update Report set testID=" + rd.getTest().getTestID()
                    + ",pID=" + rd.getPatientDetails().getpID() + ",dID=" + dID + ",report='" + rd.getReport()
                    + "', status=" + rd.getStatus() + ",testDate='" + sdf.convertDateToString(rd.getTestDate()) + "' where reportID=" + rd.getReportID();
        } else if (dID <= 0 && sID > 0) {
            qry = "update Report set testID=" + rd.getTest().getTestID()
                    + ",staffID=" + sID + ",pID=" + rd.getPatientDetails().getpID() + ",report='" + rd.getReport()
                    + "', status=" + rd.getStatus() + ",testDate='" + sdf.convertDateToString(rd.getTestDate()) + "' where reportID=" + rd.getReportID();
        } else {
            qry = "update Report set testID=" + rd.getTest().getTestID()
                    + ",pID=" + rd.getPatientDetails().getpID() + ",report='" + rd.getReport()
                    + "', status=" + rd.getStatus() + ",testDate='" + sdf.convertDateToString(rd.getTestDate()) + "' where reportID=" + rd.getReportID();
        }
        boolean pass = cnn.executeQuery(qry);
        if (pass) {
            removeReport(rd.getReportID());
            reportList.add(rd);
            resetGUI();
            optionMessage.showMessage("Report has been updated!");
            return true;
        } else {
            optionMessage.showErrorMessage("Report has not been updated! Try again later!");
            return false;
        }
    }

    private void removeReport(int reportID) {
        for (ReportDetails reportDetails : reportList) {
            if (reportDetails.getReportID() == reportID) {
                reportList.remove(reportDetails);
                break;
            }
        }
    }

    private void resetGUI() {
        main.refreshReportTableView();
    }

    public boolean deleteReport(ReportDetails rd) {
        boolean cn = cnn.createConnection();
        qry = "delete from Report where reportID=" + rd.getReportID();
        boolean pass = cnn.executeQuery(qry);
        if (pass) {
            removeReport(rd.getReportID());

            resetGUI();
            optionMessage.showMessage("Report has been deleted!");
            return true;
        } else {
            optionMessage.showErrorMessage("Report has not been deleted! Try again later!");
            return false;
        }

    }
}
